package com.merge;

import com.common.RandomUtils;
import com.common.Sort;
import com.merge.constant.FileConstant;
import com.merge.pojo.SliceFile;
import com.merge.queue.SliceQueueManger;
import com.merge.task.MonitorSliceQueueTask;

import java.io.FileWriter;

public class MergeSortDemo {

    private SliceFile[] sliceFiles;

    public MergeSortDemo(int count) {
        init(count);
    }
    private void init(int count) {
        sliceFiles = new SliceFile[count];
        int min = 0;
        int fileNum = 100000;
        for (int i = 0; i < count; i++) {
            String fileName = FileConstant.SLICE_FILE_PROFIX + (i + 1);
            sliceFiles[i] = new SliceFile(fileName, fileNum);
            int[] sequence = RandomUtils.getRandomSequence(fileNum, min, min + fileNum);
            Sort.mergeSort(sequence, 0 , sequence.length - 1);
            min += fileNum;
            for (int j = 0; j < sequence.length; j++) {
                FileWrite.writeLine(sliceFiles[i].getPath(), sequence[j]);
            }
        }
    }

    public SliceFile[] getSliceFiles() {
        return sliceFiles;
    }

    public void setSliceFiles(SliceFile[] sliceFiles) {
        this.sliceFiles = sliceFiles;
    }

    public static void main(String[] args) {
        MergeSortDemo demo = new MergeSortDemo(10);
        SliceFile[] sliceFiles = demo.getSliceFiles();
        for (int i = 0; i < sliceFiles.length; i++) {
            SliceQueueManger.addSliceQueueMap(sliceFiles[i]);
        }
        MonitorSliceQueueTask monitorSliceQueueTask = new MonitorSliceQueueTask(1);
        Thread t1 = new Thread(monitorSliceQueueTask);
        t1.start();
    }
}
